local StringUtil = require("util.StringUtil")
local startTime = os.time();
require("util.PrintUtil")
local log = require("core.log")



--[[
spring.redis.cluster.nodes=192.168.213.169:6369,192.168.213.171:6369,192.168.213.172:6369
spring.redis.passwor=stA8E2q7Bduv5MIZ
spring.redis.index=0
]]--
--- 从配置中获取redis 集群地址
---
local redis_nodes = ngx.shared.properties:get("spring.redis.cluster.nodes");
local redis_pwd = ngx.shared.properties:get("spring.redis.password");
local redis_index = ngx.shared.properties:get("spring.redis.index");


---分割根据逗号 在根据冒号
if not redis_nodes then
    log.error("not found  redis config from consul" )
    return;

end

if not redis_pwd then
    log.error("not found  redis pwd" )
    return;

end
local redis_temp_ary =StringUtil.split(redis_nodes,",")
local serv_list = {};


for k, v in ipairs(redis_temp_ary) do
    log.info("打印找到的consul中的redis 配置")
    log.info(k, v)
    log.info("consul中的redis 配置 index:"..k.." value: "..v)
    local ip_port = StringUtil.split(v,":")
    log.info("打印找到的consul中的redis 配置")
    serv_list[#serv_list+1]={ip=ip_port[1],port=tonumber(ip_port[2])};
    log.info("ip:"..ip_port[1].." port: "..ip_port[2])
end



printTable(serv_list,1);
serv_list.name="test";
local config = {
    name = "test1",

    --
    serv_list =serv_list,
    password=redis_pwd,
}
--config["serv_list"] = serv_list;
local redis_cluster = require "resty.rediscluster";

log.info("开始初始化redis")
local red = redis_cluster:new(config)

log.info("初始化redis 结果")
if not red then
    log.info("redis 链接失败")
    return;
end

log.info("redis 链接成功");



ngx.header.content_type = "application/json";
--ngx.say("aaaaaasdfasd");
local cjson = require("cjson")

local uri = ngx.var.request_uri

------组装redis key
if uri == nil then
    log.error("没有获取到uri");
    -- ngx.say("{'errorCode':'501','errorMsg':'url不对'}");
    return
end
--ngx.say("uri: "..uri);   -----这个uri是/capacity/geye/customer/list/get?params={%22appId%22:%22aae4bc40d175fde18524%22
---需要进一步排除问号后面的的内容
index = string.find(uri,"?")
if index~=nil then
    uri = string.sub(uri,0,index-1)
end
--ngx.say("uri: "..uri);
----uri == /capacity/geye/customer/list/get
local redisKey = "group:bridge:interface:mapping:"..uri
--ngx.say("redis key is "..redisKey);

---初始化redis
---

--
--local config = {
--    name = "test3",
--
--    serv_list = {
--        --{ip="192.168.200.19", port = 7001}
--        {ip="192.168.200.19", port = 7001},
--        {ip="192.168.200.19", port = 7002},
--        {ip="192.168.200.19", port = 7003},
--
--    },
--}
local redis_cluster = require "resty.rediscluster"
local red = redis_cluster:new(config)
---选择db
ok, err = red:select(0)

if not ok then
    log.error("failed to select db:", err);
    --   ngx.say("failed to select db: ", err)
    return
end


log.info("redis select success!!");


--ngx.say("redis select result: ", ok)
--log.error("redis select result:", ok);

-----获取值

local res, err = red:get(redisKey)
if not res or res=="" or res==ngx.null then
    log.error("failed to get api info :", err);
    --   ngx.say("failed to get api info : ", err)
    ngx.log(ngx.ERR,"fail to get redis:"..redisKey) --增加参数部分

    return
end

ngx.log(ngx.ERR,"拿到redis的值  类型为:"..type(res).."真实值为:"..(tostring(res)))

--ngx.say("redis get value succ: ", ok)

--ngx.say("redis get value: ",res)

-- put it into the connection pool of size 100,
-- with 10 seconds max idle time

--ngx.say("redis keepalive succ: ", ok)


ngx.log(ngx.ERR,"拿到redis的值 \r\n 类型为:"..type(res).."真实值为:"..(tostring(res)))
ngx.log(ngx.ERR,resp)

---转成json
local jsonTable = cjson.decode(res);
--ngx.say(jsonTable.bridgeUri);
ngx.var.mypath=jsonTable.bridgeUri

----检查 bridgeUri 是否为空
if jsonTable.bridgeUri==nil or jsonTable.bridgeUri == ngx.null then
    ngx.log(ngx.ERR,"代理url bridgeUri 为空" );
end

--ngx.var.mypath="coinsrv/coin/info/list.json"

local index = string.find(ngx.var.request_uri,"?")
if index==nil  then
    ngx.var.mypath= ngx.var.mypath.."?1=1"
    ngx.log(ngx.ERR,"没有找到?号后面的url 参数" );
else
    local paramStr= string.sub(ngx.var.request_uri ,index,length);
    ngx.log(ngx.ERR,"找到？号的为止是"..index.." 尾串是"..paramStr );
    local length = ngx.var.request_uri.length
    ngx.var.mypath = ngx.var.mypath..paramStr;

    ngx.log(ngx.ERR,"截取的参数部分"..string.sub(ngx.var.request_uri ,index,length)) --增加参数部分
end


----附加version

local version = jsonTable.version
ngx.var.mypath=ngx.var.mypath.."&version="..version     --截取参数部分


local interfaceName = jsonTable.interfaceName
ngx.var.mypath=ngx.var.mypath.."&interfaceName="..interfaceName     --截取参数部分
ngx.log(ngx.ERR,"最终的url"..ngx.var.mypath) --增加参数部分
--return jsonTable.bridgeUri;
---拿到version 和 uri  bridgeUri
--[[期望得到的值是
{
    "apiName": "查询客户列表信息",
    "bridgeUri": "/rest/group/capacity/handle",
    "id": 8,
    "interfaceName": "capacity.geye.customer.list.get",  ----变量
    "requestMethod": "POST",
    "updateDate": 1618311371932,
    "uri": "/capacity/geye/customer/list/get",
    "version": "1.0.0" 。 ---版本号
}
]]--


--resp.version =


--再次发送数据


---查找redis中的值是否存在


---不存在报错


----存在 解析json



---拿到请求地址  将数据作为参数传过去

-----
local cost = os.time()-startTime
ngx.log(ngx.ERR,"cost time "..cost);
